package cn.xuan.scan.dao.query.param;

import java.util.Stack;
import java.util.StringTokenizer;
import java.util.Vector;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ParseTree {
	private static final Logger logger = LoggerFactory.getLogger(ParseTree.class);
	public static final String token_left = "[";
	public static final String token_right = "]";
	public static final String space = " ";

	public static Vector parseString(String queryString) {
		Vector vtParentChild = new Vector();
		Stack stack = new Stack();
		queryString = queryString.replaceAll("\\[", " [");
		queryString = queryString.replaceAll("\\]", "] ");
		StringTokenizer rightToken = new StringTokenizer(queryString, "]");

		while (rightToken.hasMoreElements()) {
			String strContent = (String) rightToken.nextElement();
			pushPart(strContent, stack);
			pop(vtParentChild, stack);
		}

		return vtParentChild;
	}

	private static void pushPart(String strContent, Stack stack) {
		if (!strContent.trim().equals("")) {
			StringTokenizer leftToken = new StringTokenizer(strContent, "[");
			int var3 = leftToken.countTokens();

			while (leftToken.hasMoreElements()) {
				String strContentLeft = (String) leftToken.nextElement();
				if (!strContentLeft.trim().equals("")) {
					stack.push(strContentLeft);
				}
			}

		}
	}

	private static void pop(Vector vtParentChild, Stack stack) {
		if (stack.size() > 1) {
			String popString = (String) stack.pop();
			String parentOfPop = (String) stack.peek();
			if (!popString.trim().equals("") && !parentOfPop.trim().equals("")) {
				popString = popString.trim();
				vtParentChild.add(parentOfPop + "[" + popString + "]");
			} else {
				logger.debug("Stack contains sapce error");
			}

		}
	}
}
